<template>
  <div class="administrator">
    <van-form @submit="onSubmit">
      <van-field
        left-icon="manager-o"
        v-model="number"
        placeholder="账号"
        :rules="[{ required: true, message: '请输入账号' }]"
      />
      <van-field
        left-icon="coupon-o"
        type="password"
        v-model="password"
        placeholder="密码"
        :rules="[{ required: true, message: '请输入密码' }]"
      />

      <div class="button">
        <van-button round block type="primary" native-type="submit" text="确认" />
      </div>
    </van-form>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from "@vue/reactivity";
import { userLogin } from "../../network/axios";
import { Notify } from "vant";
import { useRouter } from "vue-router";
export default {
  name: "Administrator",
  setup() {
    const router = useRouter();
    let userInfo = reactive({
      number: "",
      password: "",
      type: 0,
    });
    let loading = ref(false);
    const onSubmit = async () => {
      loading.value = true;
      const {
        data: { code, result, token },
      } = await userLogin(userInfo);
      loading.value = false;
      if (code == 200) {
        Notify({
          type: "success",
          message: "登录成功",
        });
        localStorage.setItem("token", token);
        router.push({
          name: "Approval",
        });
      } else {
        Notify({
          type: "danger",
          message: `${result}`,
        });
      }
      console.log(code, "code");
    };
    return {
      loading,
      ...toRefs(userInfo),
      onSubmit,
    };
  },
};
</script>

<style lang="scss" scoped>
.administrator {
  margin-top: 0.25rem;
  padding: 0 0.25rem;
  ::v-deep .van-cell {
    margin-top: 10px;
  }
}
</style>
